package com.sdp.irrigation.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.TokenUtils;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.db.irrigation_emp.service.IIrrigationEmpService;
import com.sdp.irrigation.entity.IrrigationEmp;
import com.sdp.right.control.LoginController;
import com.sdp.right.db.sys_emp.service.ISysEmpService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.utils.RightConstants;

@RestController
@RequestMapping("/AppLoginController")
public class AppLoginController {
	
	@Autowired
	private LoginController loginController;
	
	@Autowired
	HttpServletRequest request;
	
	@Autowired
	private ISysEmpService iSysEmpService;
	
	@Autowired
	private IIrrigationEmpService iIrrigationEmpService;
	
	@Autowired
	private RedisUtil redisUtil;
	

	@PostMapping(value = "/login")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> oldLogin(@RequestBody SysEmp sysEmp) throws Exception{
		return loginController.oldLogin(sysEmp);
	}
	
	/**
	 * 根据TOKEN获取用信息
	 * @return
	 */
	@GetMapping(value = "/info")
	public Result<?> info(){

		String token = request.getHeader(CoreConstantsEnum.header中的token参数名.getCode());
		if(StringUtils.isEmpty(token)) {
			return Result.error("授权已过期，请重新登陆");
		}
		JSONObject obj = TokenUtils.verify(token);
		
		if(obj==null || StringUtils.isEmpty(obj.get(CoreConstantsEnum.token中账号的属性名.getCode())) || StringUtils.isEmpty(obj.get(CoreConstantsEnum.token中密码的属性名.getCode()))) {
			return Result.error("登陆已过期，请重新登录");
		}
		SysEmp sysEmp = iSysEmpService.querySysEmp(obj.getString(CoreConstantsEnum.token中账号的属性名.getCode()), obj.getString(CoreConstantsEnum.token中密码的属性名.getCode()));
		if(sysEmp==null) {
			return Result.error("账号密码输入错误");
		}else if(sysEmp.getIsUse().equals(RightConstants.否.getCode())){
			return Result.error("此账号已锁定，不能登录");
		}
		
		boolean isSet = redisUtil.setMap(SysEmp.class, sysEmp.getAccountNo(), sysEmp);
		if(!isSet) {
			return Result.error("请确认缓存服务已经开启");
		}
		IrrigationEmp emp = iIrrigationEmpService.queryIrrigationEmpByEmpNo(sysEmp.getEmpNo());
		JSONObject permissionInfo = new JSONObject();
		permissionInfo.put("token", TokenUtils.token(sysEmp.getAccountNo(),sysEmp.getEmpPwd(),sysEmp.getEmpNo(),sysEmp.getEmpName()));
		permissionInfo.put("emp", emp==null?sysEmp:emp);
		permissionInfo.put("empName", sysEmp.getEmpName());
		permissionInfo.put("empNo", sysEmp.getEmpNo());
		permissionInfo.put("accountNo", sysEmp.getAccountNo());
		permissionInfo.put("profilePicture",sysEmp.getProfilePicture());
		
		return Result.ok(permissionInfo);
		
	}
	
	
}
